const Upload = require('../../libs/upload')

/**
 * 音乐模型
 */
class CollectionDemo {
    /**
     * 构造音乐对象
     * @param  {number} id       标识
     * @param  {string} name     关键词
     * @return {CollectionDemo}      搜索
     */
    constructor(id, name, cover, total, playCount) {
        this.id = id
        this.name = name
        this.cover = cover
        this.total = total
        this.playCount = playCount

    }

    static find(limit = 0) {
        if (limit > 0) {
            const list = storage.slice(0, limit)
        } else {
            const list = storage
        }

        const new_list = list.map(item => {
            const temp = {}
            Object.assign(temp, item)
                // lodash
            temp.cover = Upload.getUrl(temp.cover)
            return temp
        })

        return new_list
    }


    static findOne(id) {
        const collectionDemo = storage.find(s => s.id === id)
        if (collectionDemo) {
            const Collection = {}
            Object.assign(Collection, collectionDemo)
                // lodash
            Collection.cover = Upload.getUrl(Collection.cover)
            return Collection
        }
        return collectionDemo

    }

    delete() {
        return storage.splice(storage.indexOf(this), 1)
    }

    save() {
        storage.indexOf(this) === -1 && storage.push(this)
        return true
    }

    update() {
        return true
    }

    search(name) {
        var regex = new RegExp(name);
        var list = [];
        for (i = 0; i < storage.length; i++) {
            if (regex.test(storage[i].name)) {
                list.push(storage[i])
            }
        }
        return list
    }
}
const storage = [
    new CollectionDemo(1, '收藏', 'jyqj.png', 20, 232323),
    new CollectionDemo(2, '收藏', 'jyqj.png', 20, 232323),
    new CollectionDemo(3, '收藏', 'jyqj.png', 20, 232323),
    new CollectionDemo(4, '收藏', 'jyqj.png', 20, 232323),
    new CollectionDemo(5, '收藏', 'jyqj.png', 20, 232323),
    new CollectionDemo(6, '收藏', 'jyqj.png', 20, 232323),
    new CollectionDemo(7, '收藏', 'jyqj.png', 20, 232323),
    new CollectionDemo(8, '收藏', 'jyqj.png', 20, 232323),
    new CollectionDemo(9, '收藏', 'jyqj.png', 20, 232323),
    new CollectionDemo(10, '收藏', 'jyqj.png', 20, 232323),
    new CollectionDemo(11, '收藏', 'jyqj.png', 20, 232323),
    new CollectionDemo(12, '收藏', 'jyqj.png', 20, 232323),
    new CollectionDemo(13, '收藏', 'jyqj.png', 20, 232323),
    new CollectionDemo(14, '收藏', 'jyqj.png', 20, 232323),
    new CollectionDemo(15, '收藏', 'jyqj.png', 20, 232323),
    new CollectionDemo(16, '收藏', 'jyqj.png', 20, 232323),
    new CollectionDemo(17, '收藏', 'jyqj.png', 20, 232323),
    new CollectionDemo(18, '收藏', 'jyqj.png', 20, 232323),
    new CollectionDemo(19, '收藏', 'jyqj.png', 20, 232323),
    new CollectionDemo(20, '收藏', 'jyqj.png', 20, 232323),
]

module.exports = CollectionDemo